import { v3 } from 'cc';
import { PM } from '../../../fwk/Managers/PoolManager';
import { CMD } from "../../../fwk/puremvc/BaseCMD";
import { RCommand } from "../../../fwk/puremvc/RegistFunc";
import UI_Game from '../../../ui_gen/Game/UI_Game';
import { GameCtrl } from '../../Control/GameCtrl';
import { ItemCtrl } from '../../Control/ItemCtrl';
import { Item } from '../../Item';
import { GameModel } from '../Model/GameModel';
import { IItemInfo, ItemModel } from '../Model/ItemModel';
import { GameFacade } from '../../../fwk/puremvc/GameFacade';

@RCommand(GameFacade)
/**计时操作 */
export class TickCMD extends CMD<number> {
    do(param: number): void {
        const ballonTime = 5;
        let model = CMD.getMod(GameModel);
        if (model.isStartGame) {
            model.gameTime += param;
            if (model.gameTime >= ballonTime) {
                model.gameTime -= ballonTime;
                GameFacade.send(GenerateBallonCMD);
            }
        }
    }
}
@RCommand(GameFacade)
export class StartGameCMD extends CMD {
    async do(): Promise<void> {
        CMD.getMod(GameModel).isStartGame = true;
        for (let i = 0; i < 3; i++) {
            GameFacade.send(GenerateBallonCMD);
        }
        CMD.getCtrl(GameCtrl).com.score.string = "score:0";
        await CMD.getCtrl(UI_Game).load();
        let game = CMD.getCtrl(UI_Game);
        game.show();
        game.com.setMiddle();

    }
}
@RCommand(GameFacade)
export class GenerateBallonCMD extends CMD {
    do(param: void): void {
        if (CMD.getMod(ItemModel).infos.length >= 10)
            return;
        let itemCtrl = CMD.getCtrl(ItemCtrl).com;
        let ld = itemCtrl.ld.position;
        let ru = itemCtrl.ru.position;
        let x = Math.random() * (ru.x - ld.x) + ld.x;
        let y = Math.random() * (ru.y - ld.y) + ld.y;
        let score = Math.round(Math.random() * 5) + 3;
        let res: IItemInfo = {
            score: score,
            position: v3(x, y, 0)
        }
        CMD.getMod(ItemModel).addInfo(res);
        GameFacade.send(RefreshBallonCMD);
    }
}
@RCommand(GameFacade)
export class RefreshBallonCMD extends CMD {
    do(param: void): void {
        let ctrl = CMD.getCtrl(ItemCtrl).com;
        [...ctrl.items].forEach(e => {
            PM.Put(e.node);
        })
        ctrl.items = [];
        let model = CMD.getMod(ItemModel);
        model.infos.forEach(e => {
            let node = PM.Get(ctrl.itemPrefab);
            node.parent = ctrl.itemFolder;
            let item = node.getComponent(Item);
            item.node.position = e.position;
            item.info = e;
            item.txt.string = e.score.toFixed();
            ctrl.items.push(item);
        })
    }
}
@RCommand(GameFacade)
export class HitBallonCMD extends CMD<IItemInfo> {
    do(param: IItemInfo): void {
        CMD.getMod(GameModel).score += param.score;
        GameFacade.send(RefreshScoreCMD);
        CMD.getMod(ItemModel).removeInfo(param);
        GameFacade.send(RefreshBallonCMD);
    }
}

@RCommand(GameFacade)
export class RefreshScoreCMD extends CMD {
    do(param: void): void {
        let game = CMD.getCtrl(GameCtrl).com;
        game.score.string = `score:${CMD.getMod(GameModel).score.toFixed()}`;
    }
}